package com.tanhua.dubbo.api.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.dubbo.api.TestApi;
import com.tanhua.dubbo.mapper.OptionsMapper;
import com.tanhua.dubbo.mapper.QuestionnaireMapper;
import com.tanhua.dubbo.mapper.ResultMapper;
import com.tanhua.dubbo.mapper.SoulQuestionsMapper;
import com.tanhua.model.domain.Options;
import com.tanhua.model.domain.Questionnaire;
import com.tanhua.model.domain.Result;
import com.tanhua.model.domain.SoulQuestions;
import com.tanhua.model.mongo.TestSoul;
import com.tanhua.model.vo.OptionsVo;
import com.tanhua.model.vo.QuestionnaireVo;
import com.tanhua.model.vo.SoulQuestionsVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@DubboService
public class TestApiImpl implements TestApi {
    @Autowired
    private OptionsMapper optionsMapper;

    @Autowired
    private SoulQuestionsMapper soulQuestionsMapper;

    @Autowired
    private QuestionnaireMapper questionnaireMapper;

    @Autowired
    private ResultMapper resultMapper;


    //根据问题id找出对应的option列表
    @Override
    public List<OptionsVo> optionsList(Long questionId) {
        QueryWrapper<Options> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("question_id", questionId);
        List<Options> options = optionsMapper.selectList(queryWrapper);
        List<OptionsVo> voList = new ArrayList<>();
        for (Options option : options) {
            OptionsVo vo = OptionsVo.init(option);
            voList.add(vo);
        }
        return voList;
    }

    //根据问卷id找出对应的question列表
    @Override
    public List<SoulQuestionsVo> soulQuestionsList(Long questionnaireId) {
        QueryWrapper<SoulQuestions> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("questionnaire_id", questionnaireId);
        List<SoulQuestions> soulQuestions = soulQuestionsMapper.selectList(queryWrapper);  //找出所属问卷的所有题目
        List<SoulQuestionsVo> voList = new ArrayList<>();
        for (SoulQuestions soulQuestion : soulQuestions) {
            SoulQuestionsVo vo = new SoulQuestionsVo();
            vo.setId(soulQuestion.getId().toString());
            vo.setQuestion(soulQuestion.getQuestion());
            List<OptionsVo> options = this.optionsList(soulQuestion.getId());
            vo.setOptions(options);
            voList.add(vo);
        }
        return voList;
    }

    @Override
    public List<QuestionnaireVo> questionnaireList(List<Long> questionnaireIds, Map<Object, TestSoul> testSoulMap) {
        QueryWrapper<Questionnaire> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id", questionnaireIds);
        List<Questionnaire> questionnaires = questionnaireMapper.selectList(queryWrapper);
        List<QuestionnaireVo> voList = new ArrayList<>();
        for (Questionnaire questionnaire : questionnaires) {
            QuestionnaireVo questionnaireVo = new QuestionnaireVo();
            BeanUtils.copyProperties(questionnaire, questionnaireVo);
            questionnaireVo.setId(questionnaire.getId().toString());
            List<SoulQuestionsVo> questions = this.soulQuestionsList(questionnaire.getId());
            questionnaireVo.setQuestions(questions);
            TestSoul testSoul = testSoulMap.get(questionnaire.getId());
            questionnaireVo.setIsLock(testSoul.getIsLock());
            voList.add(questionnaireVo);
        }
        return voList;
    }

    @Override
    public List<Options> findAllOption(List<Long> optionIdList) {
        QueryWrapper<Options> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id", optionIdList);
        List<Options> optionsList = optionsMapper.selectList(queryWrapper);
        return optionsList;
    }

    @Override
    public Result getResultByScore(Integer totalScore,Long questionnaireId) {
        Result result = resultMapper.getResultByScore(totalScore,questionnaireId);
        return result;
    }

    @Override
    public Long getQuestionnaireId(Long questionId) {
        QueryWrapper<SoulQuestions> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",questionId);
        SoulQuestions soulQuestions = soulQuestionsMapper.selectOne(queryWrapper);
        return soulQuestions.getQuestionnaireId();
    }

    //根据报告中的结果id获得结果
    @Override
    public Result findByResultId(Long resultId) {
        QueryWrapper<Result> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",resultId);
        Result result = resultMapper.selectOne(queryWrapper);
        return result;
    }
}
